<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<title>售票页面</title>
<link rel="stylesheet" href="${ctx}/static/plug/weui/css/weui.css"
	media="all">

<link rel="stylesheet" href="${ctx}/static/plug/weui/css/weuix.css"
	media="all">
	
	<link rel="stylesheet" href="${ctx}/static/css/PC/menuIndex.css"
	media="all">
<style type="text/css">
</style>



</head>
<body>
	<div class="header">
		
		<div class="datetime">
			<span class="times"></span>
		</div>
	</div>
	<div class="subjects">
		<div id="ticket_purchase">购票</div>
		<div id="pick_up_ticket">取票</div>
	</div>
	
	<div class="tailer">
		
	</div>
	
</body>












<script src="${ctx}/static/js/jquery/jquery-3.2.1.min.js"></script>
<script src="${ctx}/static/js/common.js"></script>
<script src="${ctx}/static/js/aes.js"></script>
<script src="${ctx}/static/plug/weui/js/zepto.weui.js"
	type="text/javascript"></script>
<script src="${ctx}/static/plug/weui/js/zepto.min.js"
	type="text/javascript"></script>


<script>
	var submission = true;
	var socket;
	var deviceNos = "";
	$(function() {
		webSocketConnect();
		function webSocketConnect() {
			if(localStorage.serverIP == null || localStorage.serverPort == null){
				 $.login({
	                 title: '设备连接',
	                 text: '',
	                 username: '',  // 默认
	                 password: '',  // 默认密码
	                 onOK: function (username, password) {
	                	 serverConnect(username,password)
	                 },
	                 onCancel: function () {
	                	 webSocketConnect();
	                 }
	             });
			}else {
				serverConnect(localStorage.serverIP,localStorage.serverPort)
			}
		}
		
		function serverConnect(serverIP,serverPort) {
       	 var host = "ws://" + serverIP + ":" + serverPort + "/"
       	 console.log(serverIP)
       	 console.log(serverPort)
       	 console.log(host)
         socket = new WebSocket(host);
       	 console.log(socket)
       	 try {

                socket.onopen = function (msg) {
                    localStorage.serverIP = serverIP;
                    localStorage.serverPort = serverPort;
                    console.log(msg)
                    
                };

                socket.onmessage = function (msg) {
                    if (typeof msg.data == "string") {
                		var data = $.parseJSON(msg.data);
                		console.log(data);
                		var deviceNo = data.device_id;
                		console.log(submission);
                		if(submission){
                    		var idDeviceNo = data.id_device_id;
                    		var qrDeviceNo = data.qr_device_id;
                    		var appId = data.park_id;
                    		var params = {
                    				"deviceNo": deviceNo,
                    				"idDeviceNo": idDeviceNo,
                    				"qrDeviceNo": qrDeviceNo,
                    				"appId": appId,
                    		}
                    		params = aesHex(JSON.stringify(params));
                    		console.log(params);
                    		$.post("${ctx}/pc/automatic/getScenicSpot", {"params":params} , function(obj) {
								if(obj.hardwareInfo){
									submission = false;
									localStorage.deviceNo = deviceNo;
									deviceNos = deviceNo;
								}else {
									$.alert("校验失败","通知",function(){
					                });
								}	                         			
                    		});
                		}
                    }
                    else {
                        alert("非文本消息");
                    }
                };

                socket.onclose = function (msg) { 
                	  $.confirm("连接设备失败是否继续连接", "提示", function() {
                		 localStorage.removeItem('serverIP');
                		 localStorage.removeItem('serverPort');
                  		 webSocketConnect();
                       }, function() {
                           //取消操作
                       });
                };
            }
            catch (ex) {
                log(ex);
                localStorage.removeItem('serverIP');
       		 	localStorage.removeItem('serverPort');
                webSocketConnect();
            }
		}
		
		getNowFormatDate();
		$("#ticket_purchase").click(function() {
			if(deviceNos){
				var params = {
	        			"deviceNo" : deviceNos
	        	};
	        	params = aesHex(JSON.stringify(params));
				window.location.href="${ctx}/pc/automatic/toVoteTicket?params="+params;
			}else{
				$.alert("初始化校验失败，该设备暂时无法使用！请联系管理员！","提示",function(){
           		    localStorage.removeItem("serverIP");
           		 	localStorage.removeItem("serverPort");
           			localStorage.removeItem("deviceNo");
           			webSocketConnect(); 
           	  	});
			}
		});
		$("#pick_up_ticket").click(function() {
			if(deviceNos){
				window.location.href="${ctx}/pc/pickUp/toCheckCodePick";
			}else{
				$.alert("初始化校验失败，该设备暂时无法使用！请联系管理员！","提示",function(){
           		  	webSocketConnect(); 
           		    localStorage.removeItem("serverIP");
           		 	localStorage.removeItem("serverPort");
           			localStorage.removeItem("deviceNo");
           	  	});
			}
		});
		
		
	});
	function getNowFormatDate() {
	    var date = new Date();
	    var seperator1 = "-";
	    var seperator2 = ":";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    var secondStr = date.getSeconds();
		var minuteStr = date.getMinutes();
		var hourStr = date.getHours();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    if (secondStr >= 0 && secondStr <= 9) {
			secondStr = "0" + secondStr;
		}
		if (minuteStr >= 0 && minuteStr <= 9) {
			minuteStr = "0" + minuteStr;
		}
		if (hourStr >= 0 && hourStr <= 9) {
			hourStr = "0" + hourStr;
		}
		
		var currentdate = date.getFullYear() + seperator1 + month + seperator1
		+ strDate + " " + hourStr + seperator2 + minuteStr
		+ seperator2 + secondStr;
	    $(".times").text(currentdate);
	    setTimeout(getNowFormatDate, 1000);
	}
</script>


</html>